package com.chunuo.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.commonutils.JwtUtils;
import com.chunuo.commonutils.R;
import com.chunuo.commonutils.ResultCode;
import com.chunuo.commonutils.ordervo.CourseWebVoOrder;
import com.chunuo.commonutils.ordervo.UcenterMemberOrder;
import com.chunuo.order.entity.Order;
import com.chunuo.order.feign.CourseFrontOpenFeign;
import com.chunuo.order.feign.UcenterMemberOpenFeign;
import com.chunuo.order.service.OrderService;
import com.chunuo.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Autowired
    private OrderService orderService;



    /**
     * 创建订单
     * @param courseId
     * @return
     */
    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable("courseId")String courseId, HttpServletRequest request){
        //获取用户的id
        String userId = JwtUtils.getMemberIdByJwtToken(request);

        if(!StringUtils.isEmpty(userId)){
            //创建订单号
            String orderNo = orderService.createOrder(courseId,userId);
            return R.ok().data("orderNo",orderNo);
        }
        return R.errorCustom().code(ResultCode.ERRORToken.getCode()).message(ResultCode.ERRORToken.getMessage());
    }

    @GetMapping("/getOrderNo/{orderNo}")
    public R getOrderNo(@PathVariable("orderNo")String orderNo){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_no",orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }


    /**
     * 是否购买了课程
     * @param courseId
     * @return
     */
    @GetMapping("getIsBuy/{courseId}")
    public R getIsBuy(@PathVariable("courseId")String courseId, HttpServletRequest request){
        boolean flag = orderService.getIsBuy(courseId,JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("isBuy",flag);
    }

    @GetMapping("getIsBuy/{courseId}/{userId}")
    public R getIsBuy(@PathVariable("courseId")String courseId,@PathVariable("userId")String userId){
        boolean flag = orderService.getIsBuy(courseId,userId);
        return R.ok().data("isBuy",flag);
    }


}

